package cn.com.elite;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Chaxun5314Thread implements Runnable{
	private static final Log log=LogFactory.getLog(Chaxun5314Thread.class);
	private boolean flag=true; //停止线程标记 
	private static int HZ=300;
	private static String url="";
	
	
	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		Chaxun5314Thread.url = url;
	}

	public int getHZ() {
		return HZ;
	}

	public void setHZ(int hz) {
		HZ = hz;
	}
	private void dorun() throws Exception{
		String sql="select appl_tb_list from appl_tb_list where query_status=2 or query_status is null";
		List list=ISPayServlet.ebean.chaxun_sql1(sql);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map=(Map)list.get(i);
				String appl_tb_list=(String)map.get("appl_tb_list");
				String xml="<?xml version='1.0' encoding='GBK'?><REQUEST><LIST>"+appl_tb_list+"</LIST></REQUEST>";
				String returnxml=ISPayServlet.ISPayConnection1(url,"","",xml,"protocol","01","packet");
				log.debug("returnxml:"+returnxml);
				Document doc=DocumentHelper.parseText(returnxml); 
				Element root=doc.getRootElement(); 
				Element LIST_eml=root.element("LIST");
				Element MEG_CODE_eml=root.element("MEG_CODE");
				Element MEG_DESC_eml=root.element("MEG_DESC");
				String List=LIST_eml.getText();
				log.debug("List:"+List);
				String MEG_CODE=MEG_CODE_eml.getText();
				log.debug("MEG_CODE:"+MEG_CODE);
				log.debug("MEG_CODE.length():"+MEG_CODE.length());
				MEG_CODE=MEG_CODE.replace(" ", "");
				log.debug("MEG_CODE_new:"+MEG_CODE);
				log.debug("MEG_CODE_new.length():"+MEG_CODE.length());
				String MEG_DESC=MEG_DESC_eml.getText();
				log.debug("MEG_DESC:"+MEG_DESC);
				String result=ISPayServlet.ebean.sp_appl_tb_query_return(List, Integer.parseInt(MEG_CODE), MEG_DESC);
				log.debug("result:"+result);
				if(result.equals("1")){
					Element import_apps=root.element("DETAILS");
					Iterator iters=import_apps.elementIterator();
					while(iters.hasNext()){
						Element app=(Element)iters.next();
						Element APPNO_Element=app.element("APPNO");
						String APPNO=APPNO_Element.getText();
						log.debug("APPNO:"+APPNO);
						Element STATUS_Element=app.element("STATUS");
						String STATUS=STATUS_Element.getText();
						log.debug("STATUS:"+STATUS);
						Element ERR_CODE_Element=app.element("ERR_CODE");
						String ERR_CODE=ERR_CODE_Element.getText();
						log.debug("ERR_CODE:"+ERR_CODE);
						Element ERR_DESC_Element=app.element("ERR_DESC");
						String ERR_DESC=ERR_DESC_Element.getText();
						log.debug("ERR_DESC:"+ERR_DESC);
						int STATUS_result=0;
						if(STATUS.equals("Y")){
							STATUS_result=1;
						}
						String result1=ISPayServlet.ebean.sp_appl_tb_Tongbu(APPNO, STATUS_result, ERR_CODE, ERR_DESC);
						log.debug("result1:"+result1);
					}
				}
			}
		}
	}
	public void run(){
		while(flag){
			try{
				dorun();
			}catch(Exception e){
				log.error("run():"+Util.getERRO(e));
			}
			try{
            	Thread.sleep(HZ*1000);
            }catch(Exception e){
            	log.error("定时错误:"+Util.getERRO(e)); 
            }
		}
		
	}
}
